perm filename TALK[ALS,ALS] blob
sn#491646 filedate 1980-01-12 generic text, type C, neo UTF8
COMMENT ā VALID 00002 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 PROGRAMMING
C00026 ENDMK
Cā;
PROGRAMMING
To all too many people, the computer is viewed as a magical device and the
computer programmer is viewed as a wizzard. Neither of these views is correct.
However, when one attempts to debunk the COMPUTER by pointing out that is a very
literal-minded device, really nothing more than a glorified adding machine, then
this makes the programmer appear to be even more of a wizzard. So how am I to
dispell these mistaken views?
In thinking about this matter of presentation, it seemed to me that if I were to
take you through the steps of writing a computer program, step by step, then
perhaps I could show you just how simple minded the computer is and at the same
time I could show you how one copes with this simple-mindedness. So that is
what I am going to do. But what should the problem be? I have decided to go
back a long way and consider a problem that was formulated by an Italian in the
year 1202. Leonardo Pisano, later called Fibonacci (from the Latin,
filius Bonaccii, the son of Bonaccio, asked the question, "How many rabbits will
be produced from a single pair of rabbits in one year". To make the problem
definite he assumed that each pair of rabbits produced one pair of offspring
each month, that the each pair became fertile at age one month, and finally
that none of the rabbits died during the period of interest. Now why do I chose
this problem?
Several people have suggested that I tell you how to write a program to compute
one's income tax, surely this should interest every one at this time of year. I
am not going to do this for the same reason that I have never written such a
program for my own use. The trouble is that this task is not
difficult conceptually ,but it is beset with a lot of gruesome detaill. I could
only make a small start on the task and then I would leave you more confused
than you are right now. There is another reason, usually a computer programmer
is asked to write a program for a task that is new to him and a large part of
his task is that of understanding the problem and of reducing it to simple
terms, simple enough for the computer to deal with it. So we are going to go
through this task togather.
Fibonacci did not propose this problem as a practical application of mathematics
to biology or the population explosion. Fibonacci, who was the greatest
European mathematician before the Renaissance, was interested in the properties
of numbers. This is an interesting problem that can be solved by simple
addition and it is much favored for use in beginning programming classes. So we
are going to write a program to generate a sequence of numbers known as the
Fibonacci series.
This same sequence also appeared in the work of Kepler in 1611, in connection
with "phyllotaxis" the study of the arrangement of leaves anf flowers in plant
life. For example, if one counts the petals on a daisy or the rows of kernels
on an ear of corn, usually one will find that this number will be one of the
numbers in the Fibonacci series. The reason why this is so has engaged the
attention of many people and it is still not completely understood. I am
tempted to tell you about the work of Lame in 1844 and of Lucas, who spent some
fifty years studying the Fibonacci series, but I must get on to my main task,
that of writing a computer program to develop this series.
Unfortunately, I must first spend a few moments talking about Operating Systems,
Editors and Compilers. The problem is, how does one communicate with a
computer?
In the very early days, when I started working on computers, the only
thing that the computer knew how to do was to read four numbers off of a punched
card, and then to do what these four numbers told it to do. So the first four
numbers on the first card that one put into the card hopper had to tell the
computer how to proceed to read the rest of the numbers on this card, and these
numbers then told the computer how to procede. This first card was, in effect,
the entire operating system. Not only that , but these numbers and the numbers
on the following cards had to all be expressed in the binary number system.
This proved to be an extemely tedious thing to do and one in which the average
person made so many mistakes that it was a very difficult task indeed to enter a
program into the computer.
So the very next thing that was done was to write a simple program that told the
the computer how to read numbers in the ordinary base ten number system and to
convert these numbers into the base two system that the computer understood.
This program was permanently stored in the computers memory. This was the
beginning of what is now known as the Operating System. When this had been done
one no longer communicated with the computer directly but rather one
communicated with the operating system. For all practical purposes the computer
with its operating system became a new sort of computer.
The next step was to add further instructions to the operating system telling
the computer how to read letters and how to convert them into numbers and how to
do the conversion in the other direction so that the computer could type out in
English. Having done this it was then possible to assign names to the computer
instructions, and to write yet another special program that could read
instructions typed in words and convert these words into those dreary old binary
numbers that the computer likes. But there is a lot of other tedious work that
is involved in writing programs, for one thing, someone must decide where in
memory the program is to be stored and where every bit of information that the
program uses is also to be stored. It wasn't long before much of this work was
also assigned to the computer and the operating system and its associated
service programs became even more complicated. Modern operating systems are
left running continuously so that they are always ready to interpret what one
types in and to render many other services.
For awhile, the only way that one could communicate with the computer was through
the use of punch cards, and the computer reported its results either by
punched cards ore by a printed record. Soon this was found to be too combersome
and a television-like screen output and typewriter keyboard input were added.
I could go on enumerating the various steps that have been taken to make the
computer easier to use but perhaps enough has been said for you to realize that
when one now sits down before a commputer terminal one is calling into use the
work of literally hundreds of other individuals who have spent hunderds of hours
writing programs that one uses unknowingly and that makes ones task relatively
easy. The first important fact that a programmer must learn is that he cannot
and need not know all of the internal workings of the computer and its service
routines in order to use it just as one need not know much about the inner
workings of an automobile to drive one. So perhaps, if there are some things
that you do not understand, you can console yourselves with the fact that you
are in good company.
So we are going to write a program. I sit down before my terminal in my study,
I turn the power on and I take the receiver of the hook and dial the computer.
When it answers, and I know this by the whistle that it emits, I throw a switch
on my terminal which connects the terminal to the telephone line so that I can
hang up the receiver without losing the line. I type the letter L with a
carriage return which is shorthand for LOGIN to tell the computer that I want to
use it for some serious work. I then type my identifying initials and finally a
secret password to identify myself.
Now I must tell the computer what I want to do. In this case I am going to
write a program. This means that I want to start a special program called an
editor and that I want this program to listen to what I now will type on
my terminal and to store the text away in a special file. So I type
CET FIBON.PAS
with a carriage return. When I do this I am supplying information to the
operating system program which interprets my key strokes into control
information. The C means that I want to create a new file using the editor that
is called by the letters ET and the name of this new file is to be Fibon, for
Fibonacci and the PAS means that I am going to write this program in a language
called PASCAL. All of these abreviations are confusing at first but they save
key strokes and once learned they are very convenient.
Now I will have to stop and say something about computer languages. Almost
everyone who has ever been a serious user of a computer has thought that there
must be a better way to write computer programs than the ways that were
available. So what does he do? -- he devises another computer language. There
are well over one hundred different languages in use today. A computer language
is simply an agreed upon set of terms that can be used to express what one wants
done. The language may be quite close to the language that the computer
understands or it can be reasonably close to normal everyday language. These
languages go by all sorts of names, BASIC, SAIL, FORTRAN, COBAL, PL1, APL, LISP,
PASCAL, you name it. It is quite impossible to remember the details of more
than a few of these languages unless one uses them every day. I find that I
forget one language as soon as I learn another one. I have been using PASCAL
recently so you are going to be exposed to PASCAL. The only thing in common
with all of these languages is that they must be extremely precise and they each
require a special and different program called a compiler to translate the words
and constructions into the language that the computer understands.
So back to the rabbit problem. We are going to have to keep track of two kinds
of rabbits, or rather of rabbit pairs, those that are old enough to bear young
and the total number of rabbit pairs. We will refer to these as simply FERTILE
and TOTAL. These numbers will vary, and so they are called variables. Now
PASCAL requires that we specify all variables before they are used so that the
compiler can assige someplace in memory where these numbers are to be stored.
Some people find it helpful to think of the computer memory as an array of mail
boxes with names assigned to them and of the data that is stored in each box as
being numbers written on slips of paper that are put into these boxes with the
proviso that only one slip can be in each box at any one time. So we must write
the following:
var FERTILE, TOTAL : integer;
which is the PASCAL way of saying that there are two variables and that they are
simply whole numbers which PASCAL calls integers. The computer language PASCAL,
in common with most computer languages, uses punctuation in a rather funny way.
This takes a bit of practice, and the only sensible thing to do is to simply
copy some other program and do what it does. Unfortunately, a single missing
semicolon or an extra on in some places will prevent a program from compiling
and so from working.
But we are getting ahead of ourselves, we have not yet told the computer what it
is that we are writing. It is true that we have implied that it is to be a
Pascal program by the name of the file that we are creating, but we must be
specific. So the first thing that we must type is
program FIBONACCI;
We have forgotten something else, Pascal is really quite simple minded and gets
confused if we do not do things in the right order. We must first tell it if
there are any numbers that are constant before we specify the variables. There
are at least two such numbers, these being the initial number of fertile pairs
and the initial total number of pairs. So we must type:
const F0 = 0; T0 = 1;
if we are to assume that we started with newly born rabbits. We could, of
course, assigned more descriptive names to these quantities, it really does not
matter what we call them so long as we always use exactly the same names. Now
we are ready to type the program proper and Pascal wants this to begin with the
word "begin". So what we have so far is:
program FIBONACCI;
const F0 = 0; T0 = 1;
var FERTILE, TOTAL : integer;
begin
Before I write the first line of code I realize that I have forgotten something
else. We will want to keep track of the months and we will have to tell the
computer when to stop. While we are at it let's carry the calculation along for
48 months instead of only 12. This means that we must specify another constant
and assign a name for another variable. In thinking ahead, I realize that we
will also need some place to hold a number temperarily and perhaps we should
call this TEMP. So now we have:
program FIBONACCI;
const F0 = 0; T0 = 1; LAST = 48;
var FERTILE, TOTAL, TEMP, MONTH : integer;
begin
We must first set up the initial conditions which we do by writing:
FERTILE := F0; TOTAL := T0; MONTH := 0;
And we will want the program to report these conditions so we write:
writeln (OUTPUT,FERTILE:14,TOTAL:14);
which says write a line into a file called OUTPUT, containing the value
now stored in location FERTILE, allowing 14 spaces for this number then
the value stored in the location TOTAL, again allowing 14 spaces for this
number.
Now, let's consider the main part of the program. If at any particular time
we have a given number of fertile pairs then at the end of the month the total
number of rabbit pairs will be increased by the number of psairs equal to the
number of fertile pairs. In PASCAL we express this fact by four statements
to wit:
MONTH := MONTH + 1;;
TEMP := FERTILE + TOTAL;
FERTILE := TOTAL;
TOTAL := TEMP;
You will note that I have had to use the TEMP location to save the new TOTAL
so that the old TOTAL would still be available to be moved into FERTILE,
since after a month's time all of the former pairs will now be fertile.
If we simply put this information into the program it would compute the data
for one month only and then stop. We must somehow tell it to make this
computation for several months. This we do by writing:
for MONTH := 1 to LAST do
begin
TEMP := FERTILE + TOTAL;
FERTILE := TOTAL;
TOTAL := TEMP;
end;